package com.atguigu.aiproject.exception;

import com.atguigu.aiproject.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

@RestControllerAdvice  // 全局异常处理,拦截所有@Controller注解的类.
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    public Result<?> error(BusinessException e) {
        log.warn("业务异常: {}", e.getMessage());
        return Result.error("400", e.getMessage());
    }
    
    @ExceptionHandler(SecurityException.class)
    public Result<?> error(SecurityException e) {
        log.error("安全异常: {}", e.getMessage(), e);
        return Result.error("403", e.getMessage());
    }
    
    @ExceptionHandler(IllegalArgumentException.class)
    public Result<?> error(IllegalArgumentException e) {
        log.warn("参数异常: {}", e.getMessage(), e);
        return Result.error("400", e.getMessage());
    }
    
    @ExceptionHandler(IOException.class)
    public Result<?> error(IOException e) {
        log.error("IO异常: {}", e.getMessage(), e);
        return Result.error("500", e.getMessage());
    }
    
    @ExceptionHandler(Exception.class)
    public Result<?> error(Exception e) {
        log.error("未处理异常: {}", e.getMessage(), e);
        return Result.error("500", e.getMessage());
    }
}